package no.somenvm.dreamplanners

import com.gitee.planners.api.KeyBindingAPI
import com.gitee.planners.api.ProfileAPI
import com.gitee.planners.api.ProfileAPI.plannersProfile
import com.gitee.planners.api.job.target.adaptTarget
import com.gitee.planners.core.config.ImmutableKeyBinding
import com.gitee.planners.core.player.PlayerSkill
import com.gitee.planners.module.kether.context.ImmutableSkillContext
import no.somenvm.dreamplanners.DreamPlanners.debug
import no.somenvm.dreamplanners.DreamPlanners.log
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import priv.seventeen.artist.dreampainter.api.DreamPainterKeyAPI
import priv.seventeen.artist.dreampainter.event.PlayerSimpleKeyPressEvent
import taboolib.common.platform.event.SubscribeEvent

// 按键事件
object Press {
    //注册技能按键
    fun regKeys(){
        KeyBindingAPI.toMap().forEach {
            val keys = (it.value as? ImmutableKeyBinding)?.mapping ?: return@forEach
            log.sendMessage("注册技能按键 <${it.value.name}>("+ it.key+ ") 按键序列"+ (it.value as? ImmutableKeyBinding)?.mapping)
            DreamPainterKeyAPI.registerSimpleKey(it.key,keys)
        }
    }

    @SubscribeEvent
    fun e(e: PlayerSimpleKeyPressEvent){
        debug { log.sendMessage("玩家[${e.player.name}]按下了按键[${e.keyName}]") }
        val kb = KeyBindingAPI.toMap().firstNotNullOfOrNull {
            if (it.key == e.keyName) it.value
            else null
        }
        if (kb != null) {
            e.player.plannersProfile.getRegistriedSkillOrNull(kb)?.let {
                cast(e.player,it)
            }
        }
    }

    //释放技能
    fun cast(player: Player, skill: PlayerSkill){
        val skill = ImmutableSkillContext(player.adaptTarget(),skill.immutable,skill.level)
        skill.run()
    }

//    @SubscribeEvent
//    fun e(e: PluginReloadEvents){
//        submitAsync(delay = 20) {
//            regKeys()
//        }
//    }
}